/*
 * Copyright (C) 2012 Roman Yakovenko <b179a9@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.zi.gui.component.element;

import com.zi.gui.provider.AutocompleteProvider;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Created by b17
 * 3/9/12 7:06 PM
 */

public class AutocompleteElement extends JFrame implements DocumentListener, KeyListener, ListSelectionListener {
    
    private static final int WIDTH = 250;
    
    private static final int HEIGHT = 40;

    private AutocompleteProvider provider;

    private JTextField textField;

    private JList completeList;

    private JScrollPane completeListPane;

    public AutocompleteElement(final AutocompleteProvider provider) {
        super();
        this.provider = provider;
        render();
        registerListeners();
        setBounds(150, 250, WIDTH, HEIGHT);
        setUndecorated(true);
    }

    private void render() {
        BoxLayout layout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS);
        getContentPane().setLayout(layout);
        add(new JLabel("Press Enter to continue or ESC to exit."));

        textField = new JTextField();
        textField.setText("Input ip address.");
        textField.setFocusable(true);
        textField.setFocusCycleRoot(true);
        add(textField);

        DefaultListModel model = new DefaultListModel();
        completeList = new JList(model);
        completeListPane = new JScrollPane(completeList);
        completeList.setFocusable(true);
        completeList.setFocusCycleRoot(true);
        add(completeListPane);
        completeListPane.setVisible(false);
    }

    private void registerListeners() {
        textField.getDocument().addDocumentListener(this);
        addKeyListener(this);
        textField.addKeyListener(this);
        completeList.addListSelectionListener(this);
    }

    private void autocomplete() {
        String[] propose = provider.find(textField.getText());
        completeListPane.setVisible(propose.length > 0);
        DefaultListModel model = (DefaultListModel) completeList.getModel();
        model.clear();
        for (String match : propose) {
            model.addElement(match);
        }
        pack();
    }

    public String getValue() {
        return textField.getText();
    }

    @Override
    public void insertUpdate(DocumentEvent documentEvent) {
        autocomplete();
    }

    @Override
    public void removeUpdate(DocumentEvent documentEvent) {
        autocomplete();
    }

    @Override
    public void changedUpdate(DocumentEvent documentEvent) {
        autocomplete();
    }

    @Override
    public void keyTyped(KeyEvent keyEvent) {

    }

    @Override
    public void keyPressed(KeyEvent keyEvent) {

    }

    @Override
    public void keyReleased(KeyEvent keyEvent) {
        if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
            provider.save(getValue());
            setVisible(false);
        }

        if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE) {
            setVisible(false);
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent listSelectionEvent) {
        textField.setText((String) completeList.getSelectedValue());
        textField.requestFocusInWindow();
    }
}
